Ext.define('cp.view.form.AdminBase',{
   extend: 'Ext.form.Panel',
   
    bodyPadding: '5px 5px 5px 5px', 
    bodyStyle: 'border: none',
    fieldDefaults: {
        labelWidth: 65,
        labelAlign: 'top'
    },
   
   initComponent: function(){
       
       var action;
       
       var infoPersonal = [
            { fieldLabel: 'Apellido', name: 'apellido', allowBlank: false, minLength: 3, 
                maxLength: 30, vtype: 'letrasEspacios'},
            { fieldLabel: 'Nombre', name: 'nombre', allowBlank: false, minLength: 4,
                maxLength: 30, vtype: 'letrasEspacios'},
            { fieldLabel: 'Domicilio', name: 'domicilio', allowBlank: false, minLength: 9, 
                maxLength: 200, vtype: 'domicilio' },
            { fieldLabel:  'Nº Documento', name: 'dni', xtype: 'textfield', allowBlank: false, 
                minLength: 8, maxLength: 10, vtype: 'identificacion'},
            { fieldLabel:  'Fecha de nacimiento', name: 'fechaNacimiento', xtype: 'datefield', 
                allowBlank: false, format: 'Y-m-d'}
        ];
        
        var infoContacto = [
            { fieldLabel: 'Tel Casa', name: 'contacto-2', allowBlank: true, vtype: 'telefono' },
            { fieldLabel: 'Tel Movil', name: 'contacto-3', allowBlank: true, vtype: 'telefono' },
            { fieldLabel: 'Tel Trabajo', name: 'contacto-1', allowBlank: true, vtype: 'telefono' },
            { fieldLabel: 'Tel Otro', name: 'contacto-4', allowBlank: true, vtype: 'telefono' },
            { fieldLabel: 'E-mail', name: 'email', allowBlank: true, vtype: 'email' }            
        ];
        
        var infoCuenta = [
            { fieldLabel: 'Usuario', name: 'nombreUsuario', allowBlank: false, minLength: 8, maxLength: 16 },
            { inputType:'password' ,fieldLabel: 'Clave', name: 'clave', allowBlank: false, minLength: 8, maxLength: 16 },
            { inputType:'password' ,fieldLabel: 'Repetir clave', name: 'reClave', allowBlank: false, minLength: 8, maxLength: 16},
        ];
        
        this.items = [
            {
                bodyStyle: 'border: none',
                layout: 'column',
                items: [
                    {
                        xtype: 'fieldset', defaultType: 'textfield', title: 'Personal', 
                        layout: 'column', items:
                        [
                            {xtype: 'fieldset', defaultType: 'textfield', style: 'border: none',
                                items: infoPersonal}
                        ]
                    },
                    {xtype: 'fieldset', defaultType: 'textfield', style:'margin-left: 2px',
                        title: 'Contacto', items: infoContacto},
                   {xtype: 'fieldset', defaultType: 'textfield', style:'margin-left: 2px',
                        title: 'Cuenta', items: infoCuenta}
              ]
            }
         ];  
        
       this.callParent(arguments);
    },
    submitForm: function(){
        if(this.getForm().isValid()){
            this.sendData();
        }else{
            Ext.create('cp.view.ui.AlertErr',{
                mensaje: 'El formulario contiene errores. Por favor, corrigalos, y vuelva a realizar la operación.'
                    }).showDialog();    
        }
    },
    sendData: function(){
        var me = this;
        this.getForm().submit({
            url: me.action,
            method: 'POST',
            success: function(form,action){
                Ext.create('cp.view.ui.AlertOk',{mensaje:action.result.msg}).showDialog();
                me.up('decoratorForm').close();
                Ext.ComponentQuery.query('gestionAdmins')[0].getStore().load();                 
            },
            failure: function(form, action){
                Ext.create('cp.view.ui.AlertErr',{mensaje:action.result.msg}).showDialog();
            }
        });
    }   
});